import os
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

import constants

class BaseHandler(webapp.RequestHandler):
  template_values={}
  is_login=False
  user=None
  
  def send_to_template(self,template_file,template_values):
      path=os.path.join(os.path.dirname(__file__), template_file)
      self.response.out.write(template.render(path, template_values))
  
  def list_all_log(self,user,template_values):
      logs = db.GqlQuery("SELECT * FROM Log WHERE Log.user = :1",user)
      template_values["logs_list"] = logs
  
  def user_do(self,name,email):
      pass
  
  def non_user_do(self):
      self.send_to_template("login.html",self.template_values)
      
  def get(self):
    self.user = users.get_current_user()
    
    if not self.user:      
        self.template_values[constants.Login_URL]=users.create_login_url("/")
        self.non_user_do()
        
    else:
        user_email=self.user.email
        try:
            user_name=self.user.nikename()
        except Exception:
            user_name=self.user.email()
        
        self.template_values [constants.User_Name] = user_name;
        self.template_values [constants.User_Email] = user_email
        self.template_values [constants.Logout_URL] = users.create_logout_url("/")
        self.isLogin=True
        self.user_do(user_name,user_email)